package com.work.service.main.impl;


import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.common.cache.EsignCache;
import com.work.constant.ExceptionDescription;
import com.work.constant.TallyType;
import com.work.dto.main.*;
import com.work.entity.main.OneStockTally;
import com.work.entity.main.ShipBillStorage;
import com.work.mapper.main.OneStockTallyMapper;
import com.work.service.main.*;
import com.work.service.transport.ITransportService;
import com.work.vo.ShiftInfoForAssignWorkVO;
import com.work.vo.main.*;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.DictBizCache;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 单垛理货接口实现
 *
 * @author ：dujianyu
 * @since ：Created in 2024/3/12 8:52
 */
@Service
@AllArgsConstructor
public class OneStockTallyServiceImpl extends BaseServiceImpl<OneStockTallyMapper, OneStockTally> implements IOneStockTallyService {

    private ITransportService transportService;
    private ITShipBillService itShipBillService;
    private ITShipForecastService itShipForecastService;
    private ITShipBillStatusService itShipBillStatusService;
    private IShipBillStorageService iShipBillStorageService;
    private ITallyPassService iTallyPassService;
    private ITShipBillRemarkService itShipBillRemarkService;
    private ITallyMachineService tallyMachineService;

    @Override
    @DSTransactional
    public boolean submitOneStockTally(OneStockTallyAppSubmitDTO dto) {
        OneStockTally entity = BeanUtil.copy(dto, OneStockTally.class);

        if (Func.isNull(entity)) {
            throw new RuntimeException(ExceptionDescription.FUN_ERROR + "submitOneStockTally-BeanUtil.copy复制失败");
        }

        if (!Func.isNull(entity.getTallyPackageType())) {
            entity.setTallyPackageTypeName(transportService.getPackageTypeNameByCode(entity.getTallyPackageType()));
        }

        entity.setUpdateUserName(AuthUtil.getUserName());

        if (!Func.isNull(entity.getId())) {
            OneStockTally detail = detail(entity.getId());
            if (!Func.isNull(detail.getIsCheck())) {
                return saveTallyBaseInfo(entity,dto);
            }
        }

        entity.setCheckPiece(entity.getTallyPiece());
        entity.setCheckTon(entity.getTallyTon());

        if (Func.isNull(entity.getTShipBillId())) {
            entity.setBatchId(UUID.randomUUID().toString());
            entity.setCreateUserName(AuthUtil.getUserName());

            Long newTshipBillId = itShipBillService.addTransportBill(entity.getTransportShipId(), entity.getTransportBillId());
            entity.setTShipBillId(newTshipBillId);

            if (!itShipForecastService.addShipForecast(entity.getTransportShipId())) {
                throw new RuntimeException(ExceptionDescription.DB_ERROR + "添加理货船舶信息失败");
            }

            if (!itShipBillStatusService.addTshipBillStatus(newTshipBillId, entity.getTransportShipId(), entity.getTransportBillId())) {
                throw new RuntimeException(ExceptionDescription.DB_ERROR + "添加理货舱单状态失败");
            }
        }


        switch (entity.getTallyType()) {
            case TallyType.IN:
                ShipBillStorage storage = setStorage(entity, dto);
                storage.setIsActive("Y");
                storage.setActiveInfo("NORMAL");
                Long newAffectStorageId = iShipBillStorageService.addNewShipStorage(storage);

                entity.setAffectStorageId(newAffectStorageId);
                if (!Func.isNull(entity.getId())) {
                    OneStockTally oldTallyPieceTon = detail(entity.getId());
                    iShipBillStorageService.subStorage(oldTallyPieceTon.getAffectStorageId(), oldTallyPieceTon.getTallyPiece(), oldTallyPieceTon.getTallyTon());
                    itShipBillStatusService.changePieceTon(TallyType.IN, TallyType.SUB, entity.getTShipBillId(), oldTallyPieceTon.getTallyPiece(), oldTallyPieceTon.getTallyTon());

                }
                iShipBillStorageService.addStorageWithGoodPositionId(newAffectStorageId, dto.getTallyPiece(), dto.getTallyTon(), dto.getGoodsPositionId());
                itShipBillStatusService.changePieceTon(TallyType.IN, TallyType.ADD, entity.getTShipBillId(), dto.getTallyPiece(), dto.getTallyTon());
                break;
            case TallyType.OUT:
                entity.setAffectStorageId(dto.getAffectStorageId());
                if (!Func.isNull(entity.getId())) {
                    OneStockTally oldTallyPieceTon = detail(entity.getId());
                    iShipBillStorageService.addStorage(oldTallyPieceTon.getAffectStorageId(), oldTallyPieceTon.getTallyPiece(), oldTallyPieceTon.getTallyTon());
                    itShipBillStatusService.changePieceTon(TallyType.OUT, TallyType.SUB, entity.getTShipBillId(), oldTallyPieceTon.getTallyPiece(), oldTallyPieceTon.getTallyTon());

                }
                iShipBillStorageService.subStorageWithGoodPositionId(dto.getAffectStorageId(), dto.getTallyPiece(), dto.getTallyTon(), dto.getGoodsPositionId());
                itShipBillStatusService.changePieceTon(TallyType.OUT, TallyType.ADD, entity.getTShipBillId(), dto.getTallyPiece(), dto.getTallyTon());
                break;
            case TallyType.DIRECT:
                ShipBillStorage directStorage = setStorage(entity, dto);
                directStorage.setIsActive("N");
                directStorage.setActiveInfo("DIRECT");
                Long newDirectAffectStorageId = iShipBillStorageService.addNewShipStorage(directStorage);

                entity.setAffectStorageId(newDirectAffectStorageId);
                if (!Func.isNull(entity.getId())) {
                    OneStockTally oldTallyPieceTon = detail(entity.getId());
                    iShipBillStorageService.subStorage(oldTallyPieceTon.getAffectStorageId(), oldTallyPieceTon.getTallyPiece(), oldTallyPieceTon.getTallyTon());
                    itShipBillStatusService.changePieceTon(TallyType.DIRECT, TallyType.SUB, entity.getTShipBillId(), oldTallyPieceTon.getTallyPiece(), oldTallyPieceTon.getTallyTon());

                }
                iShipBillStorageService.addStorage(newDirectAffectStorageId, dto.getTallyPiece(), dto.getTallyTon());
                itShipBillStatusService.changePieceTon(TallyType.DIRECT, TallyType.ADD, entity.getTShipBillId(), dto.getTallyPiece(), dto.getTallyTon());
                break;
            default:
                throw new RuntimeException(ExceptionDescription.CONSTANT_ERROR + "无法确定理货类型");
        }

        return saveTallyBaseInfo(entity,dto);
    }


    @Override
    @DSTransactional
    public boolean submitOneStockTallyBatch(OneStockTallyAppSubmitBatchDTO dto) {
        if (Func.isNull(dto.getStorageList()) || Func.isEmpty(dto.getStorageList())) {
            throw new RuntimeException(ExceptionDescription.LOGIC_ERROR + "没有货垛数据");
        }

        boolean success = false;

        OneStockTally entity = BeanUtil.copy(dto, OneStockTally.class);
        if (Func.isNull(entity)) {
            throw new RuntimeException(ExceptionDescription.FUN_ERROR + "submitOneStockTally-BeanUtil.copy复制失败");
        }
        entity.setBatchId(UUID.randomUUID().toString());
        entity.setUpdateUserName(AuthUtil.getUserName());
        entity.setCreateUserName(AuthUtil.getUserName());
        if (Func.isNull(entity.getTShipBillId())) {
            Long newTshipBillId = itShipBillService.addTransportBill(entity.getTransportShipId(), entity.getTransportBillId());
            entity.setTShipBillId(newTshipBillId);

            if (!itShipForecastService.addShipForecast(entity.getTransportShipId())) {
                throw new RuntimeException(ExceptionDescription.DB_ERROR + "添加理货船舶信息失败");
            }

            if (!itShipBillStatusService.addTshipBillStatus(newTshipBillId, entity.getTransportShipId(), entity.getTransportBillId())) {
                throw new RuntimeException(ExceptionDescription.DB_ERROR + "添加理货舱单状态失败");
            }
        }

        for (OneStockTallyAppSubmitStorageDTO storageData : dto.getStorageList()) {
            entity.setId(null);

            if (!Func.isNull(storageData.getTallyPackageType())) {
                entity.setTallyPackageType(storageData.getTallyPackageType());
                entity.setTallyPackageTypeName(transportService.getPackageTypeNameByCode(storageData.getTallyPackageType()));
            }

            entity.setTallyPiece(storageData.getTallyPiece());
            entity.setTallyTon(storageData.getTallyTon());

            entity.setCheckPiece(storageData.getTallyPiece());
            entity.setCheckTon(storageData.getTallyTon());

            switch (entity.getTallyType()) {
                case TallyType.IN:
                    ShipBillStorage storage = setStorage(entity, storageData);
                    storage.setIsActive("Y");
                    storage.setActiveInfo("NORMAL");
                    Long newAffectStorageId = iShipBillStorageService.addNewShipStorage(storage);

                    entity.setAffectStorageId(newAffectStorageId);
                    storageData.setAffectStorageId(newAffectStorageId);

                    iShipBillStorageService.addStorageWithGoodPositionId(newAffectStorageId, storageData.getTallyPiece(), storageData.getTallyTon(), storageData.getGoodsPositionId());
                    itShipBillStatusService.changePieceTon(TallyType.IN, TallyType.ADD, entity.getTShipBillId(), storageData.getTallyPiece(), storageData.getTallyTon());
                    break;
                case TallyType.OUT:
                    entity.setAffectStorageId(storageData.getAffectStorageId());
                    iShipBillStorageService.subStorageWithGoodPositionId(storageData.getAffectStorageId(), storageData.getTallyPiece(), storageData.getTallyTon(), storageData.getGoodsPositionId());
                    itShipBillStatusService.changePieceTon(TallyType.OUT, TallyType.ADD, entity.getTShipBillId(), storageData.getTallyPiece(), storageData.getTallyTon());
                    break;
                default:
                    throw new RuntimeException(ExceptionDescription.CONSTANT_ERROR + "无法确定理货类型");
            }

            entity.setTallyPiece(storageData.getTallyPiece());
            entity.setTallyTon(storageData.getTallyTon());
            entity.setTallyPackageType(storageData.getTallyPackageType());

            success = saveBatchTallyBaseInfo(entity, dto);
        }

        return success;
    }

    @Override
    public IPage<OneStockTallyVO> selectOsTallyHistoryPage(IPage<OneStockTallyVO> page, OneStockTallySearchDTO dto) {
        if ("Y".equals(dto.getPersonal())) {
            dto.setCreateUser(AuthUtil.getUserId());
        }
        List<OneStockTallyVO> data = baseMapper.selectOsTallyHistoryList(dto, page);
        return page.setRecords(data);
    }

    @Override
    public boolean removeOneStockTally(Long id) {
        OneStockTally oneStockTally = detail(id);
        if (!Func.isNull(oneStockTally.getIsCheck())) {
            throw new RuntimeException(ExceptionDescription.LOGIC_ERROR + "该工班已审核");
        }
        switch (oneStockTally.getTallyType()) {
            case TallyType.IN:
            case TallyType.DIRECT:
                iShipBillStorageService.subStorage(oneStockTally.getAffectStorageId(), oneStockTally.getTallyPiece(), oneStockTally.getTallyTon());
                break;
            case TallyType.OUT:
                iShipBillStorageService.addStorage(oneStockTally.getAffectStorageId(), oneStockTally.getTallyPiece(), oneStockTally.getTallyTon());
                break;
            default:
                throw new RuntimeException(ExceptionDescription.CONSTANT_ERROR + "无法确定理货类型");
        }
        return  itShipBillStatusService.changePieceTon(oneStockTally.getTallyType(), TallyType.SUB, oneStockTally.getTShipBillId(), oneStockTally.getTallyPiece(), oneStockTally.getTallyTon())
                && super.deleteLogic(Func.toLongList(id.toString()))
                && iTallyPassService.removeByTallyId(id);
    }

    @Override
    public IPage<OneStockTallyCheckVO> selectCheckPage(IPage<OneStockTallyCheckVO> page, OneStockTallySearchDTO dto) {
        List<OneStockTallyCheckVO> data = baseMapper.selectCheckList(dto, page);
        return page.setRecords(data);
    }

    @Override
    public String changeIsCheck(String id) {
        OneStockTally detail = detail(Func.toLong(id));

        OneStockTally update = new OneStockTally();
        update.setId(Func.toLong(id));
        if ("Y".equals(detail.getIsCheck())) {
            update.setIsCheck("N");
        } else {
            update.setIsCheck("Y");
            update.setIsEdit("N");
        }
        update.setChecker(AuthUtil.getUserId());
        update.setCheckerName(AuthUtil.getUserName());
        if (!super.saveOrUpdate(update)) {
            throw new RuntimeException(ExceptionDescription.DB_ERROR + "更新检算状态失败");
        }
        return update.getIsCheck();
    }

    @Override
    public boolean correctPieceTon(OneStockTallyCorrectSubmitDTO dto) {
        OneStockTally oldTally = detail(dto.getId());

        switch (oldTally.getTallyType()) {
            case TallyType.IN:
                iShipBillStorageService.subStorage(oldTally.getAffectStorageId(), oldTally.getCheckPiece(), oldTally.getCheckTon());
                itShipBillStatusService.changePieceTon(TallyType.IN, TallyType.SUB, oldTally.getTShipBillId(), oldTally.getCheckPiece(), oldTally.getCheckTon());

                iShipBillStorageService.addStorage(oldTally.getAffectStorageId(), dto.getCheckPiece(), dto.getCheckTon());
                itShipBillStatusService.changePieceTon(TallyType.IN, TallyType.ADD, oldTally.getTShipBillId(), dto.getCheckPiece(), dto.getCheckTon());
                break;
            case TallyType.OUT:
                iShipBillStorageService.addStorage(oldTally.getAffectStorageId(), oldTally.getCheckPiece(), oldTally.getCheckTon());
                itShipBillStatusService.changePieceTon(TallyType.OUT, TallyType.SUB, oldTally.getTShipBillId(), oldTally.getCheckPiece(), oldTally.getCheckTon());

                iShipBillStorageService.subStorage(oldTally.getAffectStorageId(), dto.getCheckPiece(), dto.getCheckTon());
                itShipBillStatusService.changePieceTon(TallyType.OUT, TallyType.ADD, oldTally.getTShipBillId(), dto.getCheckPiece(), dto.getCheckTon());
                break;
            case TallyType.DIRECT:
                iShipBillStorageService.subStorage(oldTally.getAffectStorageId(), oldTally.getCheckPiece(), oldTally.getCheckTon());
                itShipBillStatusService.changePieceTon(TallyType.DIRECT, TallyType.SUB, oldTally.getTShipBillId(), oldTally.getCheckPiece(), oldTally.getCheckTon());

                iShipBillStorageService.addStorage(oldTally.getAffectStorageId(), dto.getCheckPiece(), dto.getCheckTon());
                itShipBillStatusService.changePieceTon(TallyType.DIRECT, TallyType.ADD, oldTally.getTShipBillId(), dto.getCheckPiece(), dto.getCheckTon());
                break;
            default:
                throw new RuntimeException(ExceptionDescription.CONSTANT_ERROR + "无法确定理货类型");
        }

        if (Func.isNull(oldTally.getIsCheck())) {
            oldTally.setIsCheck("N");
        }
        oldTally.setCheckPiece(dto.getCheckPiece());
        oldTally.setCheckTon(dto.getCheckTon());
        oldTally.setCheckType(dto.getCheckType());
        oldTally.setCheckReason(dto.getCheckReason());
        oldTally.setChecker(AuthUtil.getUserId());
        oldTally.setCheckerName(AuthUtil.getUserName());
        return super.saveOrUpdate(oldTally);
    }

    @Override
    public IPage<OneStockTallyUnlockVO> selectUnlockPage(IPage<OneStockTallyUnlockVO> page, OneStockTallySearchDTO dto) {
        List<OneStockTallyUnlockVO> data = baseMapper.selectUnlockList(dto, page);
        return page.setRecords(data);
    }

    @Override
    public String changeIsEdit(String id) {
        OneStockTally detail = detail(Func.toLong(id));

        OneStockTally update = new OneStockTally();
        update.setId(Func.toLong(id));
        if("Y".equals(detail.getIsCheck())) {
               throw new RuntimeException(ExceptionDescription.LOGIC_ERROR + "检算已完成");
        }
        if ("Y".equals(detail.getIsEdit()) || Func.isNull(detail.getIsEdit())) {
            update.setIsEdit("N");
        } else {
            update.setIsEdit("Y");
        }
        update.setEditChangeUser(AuthUtil.getUserId());
        update.setEditChangeUserName(AuthUtil.getUserName());
        if (!super.saveOrUpdate(update)) {
            throw new RuntimeException(ExceptionDescription.DB_ERROR + "更新可编辑状态失败");
        }
        return update.getIsEdit();
    }

    @Override
    public OneStockTallyUnlockDetailVO selectOstDetail(OneStockTallySearchDTO dto) {
        if (Func.isNull(dto.getId())) {
            throw new RuntimeException(ExceptionDescription.NO_PARAM + "缺少主键ID");
        }
        return baseMapper.selectOstDetail(dto);
    }

    @Override
    public OneStockTallyShipStatCheckDetail selectShipStatCheckDetail(OneStockTallySearchDTO dto) {
        QueryWrapper<OneStockTally> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(OneStockTally::getId,
                        OneStockTally::getTallyPiece,
                        OneStockTally::getTallyTon,
                        OneStockTally::getCheckPiece,
                        OneStockTally::getCheckTon,
                        OneStockTally::getCheckType,
                        OneStockTally::getCheckReason)
                .eq(OneStockTally::getIsDeleted, 0)
                .eq(OneStockTally::getId, dto.getId())
                .last("limit 1");
        OneStockTally entity = baseMapper.selectOne(queryWrapper);
        if (Func.isNull(entity)) {
            throw new RuntimeException(ExceptionDescription.DB_ERROR + "没有理货数据");
        }
        return BeanUtil.copy(entity, OneStockTallyShipStatCheckDetail.class);
    }

    @Override
    public IPage<OneStockTallySearchExportVO> selectTallySearchExportPage(IPage<OneStockTallySearchExportVO> page, OneStockTallySearchDTO dto) {
        List<OneStockTallySearchExportVO> data = baseMapper.selectTallySearchExportList(dto, page);
        return page.setRecords(data);
    }

    @Override
    public TotalPieceTonVO selectTallySearchExportTotal(OneStockTallySearchDTO dto) {
        return baseMapper.selectTallySearchExportTotal(dto);
    }

    @Override
    public ShiftInfoForAssignWorkVO shiftInfoByArrangeId(Long arrangeId) {
        ShiftInfoForAssignWorkVO vo = baseMapper.tallyUserAndCheckerBYArrangeId(arrangeId);
        List<ShiftInfoForAssignWorkVO> data = baseMapper.shiftInfoByArrangeId(arrangeId);
        if (vo != null) {
            vo.setData(data);
            vo.setTallySign(EsignCache.getSignByUserId(Func.toLong(vo.getTallyUser())));
            vo.setCheckSign(EsignCache.getSignByUserId(Func.toLong(vo.getChecker())));
        }
        return vo;
    }

    @Override
    public List<ShiftInfoForAssignWorkVO> listTallyInfoByArrangeId(ShiftInfoForAssignWorkVO vo) {
        List<ShiftInfoForAssignWorkVO> res = baseMapper.listTallyInfoByArrangeId(vo.getArrangeId(), vo);
        for (ShiftInfoForAssignWorkVO re : res) {
            re.setTimeSpaceName(DictBizCache.getValue("time_space", re.getTimeSpace()));
        }
        return res;
    }

    private boolean saveTallyBaseInfo(OneStockTally entity, OneStockTallyAppSubmitDTO dto) {
        if (!super.saveOrUpdate(entity)) {
            throw new RuntimeException(ExceptionDescription.DB_ERROR + "保存单垛理货失败");
        }
        return iTallyPassService.submitTallyPass(setTallySubmitDTO(entity, dto))
                && itShipBillRemarkService.submitShipBillRemark(setTshipBillRemarkSubmitDTO(entity, dto)) && tallyMachineService.submitTallyMachineBatch(entity.getId(), dto.getMachineList());
    }

    private boolean saveBatchTallyBaseInfo(OneStockTally entity, OneStockTallyAppSubmitBatchDTO dto) {
        if (!super.saveOrUpdate(entity)) {
            throw new RuntimeException(ExceptionDescription.DB_ERROR + "保存单垛理货失败");
        }
        return iTallyPassService.submitTallyPass(setTallySubmitDTO(entity, dto))
                && itShipBillRemarkService.submitShipBillRemark(setTshipBillRemarkSubmitDTO(entity, dto)) && tallyMachineService.submitTallyMachineBatch(entity.getId(), dto.getMachineList());
    }

    private ShipBillStorage setStorage(OneStockTally entity, OneStockTallyAppSubmitDTO dto) {
        ShipBillStorage storage = new ShipBillStorage();
        storage.setTShipBillId(entity.getTShipBillId());
        storage.setTransportBillId(dto.getTransportBillId());
        storage.setTransportShipId(dto.getTransportShipId());
        storage.setAreaId(dto.getStorageAreaId());
        storage.setAreaName(dto.getStorageAreaName());
        storage.setPositionId(dto.getStoragePositionId());
        storage.setPositionName(dto.getStoragePositionName());
        storage.setSpot(dto.getSpot());
        storage.setPackageType(dto.getTallyPackageType());
        storage.setPackageTypeName(entity.getTallyPackageTypeName());
        storage.setGoodsPositionId(dto.getGoodsPositionId());
        storage.setAreaCode(dto.getAreaCode());
        return storage;
    }

    private ShipBillStorage setStorage(OneStockTally entity, OneStockTallyAppSubmitStorageDTO dto) {
        ShipBillStorage storage = new ShipBillStorage();
        storage.setTShipBillId(entity.getTShipBillId());
        storage.setTransportBillId(entity.getTransportBillId());
        storage.setTransportShipId(entity.getTransportShipId());
        storage.setAreaId(dto.getStorageAreaId());
        storage.setAreaName(dto.getStorageAreaName());
        storage.setPositionId(dto.getStoragePositionId());
        storage.setPositionName(dto.getStoragePositionName());
        storage.setSpot(dto.getSpot());
        storage.setPackageType(dto.getTallyPackageType());
        storage.setPackageTypeName(entity.getTallyPackageTypeName());
        storage.setGoodsPositionId(dto.getGoodsPositionId());
        storage.setAreaCode(dto.getAreaCode());
        return storage;
    }

    private TallyPassSubmitDTO setTallySubmitDTO(OneStockTally oneStockTally, OneStockTallyAppSubmitDTO dto) {
        TallyPassSubmitDTO submitDTO = new TallyPassSubmitDTO();
        submitDTO.setTallyId(oneStockTally.getId());
        submitDTO.setTShipBillId(oneStockTally.getTShipBillId());
        submitDTO.setTransportBillId(oneStockTally.getTransportBillId());
        submitDTO.setTransportShipId(oneStockTally.getTransportShipId());
        submitDTO.setKeeper(dto.getKeeper());
        submitDTO.setKeeperName(dto.getKeeperName());
        submitDTO.setIsBedding(dto.getIsBedding());
        submitDTO.setIsCover(dto.getIsCover());
        submitDTO.setIsSafeProduction(dto.getIsSafeProduction());
        return submitDTO;
    }

    private TallyPassSubmitDTO setTallySubmitDTO(OneStockTally oneStockTally, OneStockTallyAppSubmitBatchDTO dto) {
        TallyPassSubmitDTO submitDTO = new TallyPassSubmitDTO();
        submitDTO.setTallyId(oneStockTally.getId());
        submitDTO.setTShipBillId(oneStockTally.getTShipBillId());
        submitDTO.setTransportBillId(oneStockTally.getTransportBillId());
        submitDTO.setTransportShipId(oneStockTally.getTransportShipId());
        submitDTO.setKeeper(dto.getKeeper());
        submitDTO.setKeeperName(dto.getKeeperName());
        submitDTO.setIsBedding(dto.getIsBedding());
        submitDTO.setIsCover(dto.getIsCover());
        submitDTO.setIsSafeProduction(dto.getIsSafeProduction());
        return submitDTO;
    }

    private TShipBillRemarkSubmitDTO setTshipBillRemarkSubmitDTO(OneStockTally oneStockTally, OneStockTallyAppSubmitDTO dto) {
        TShipBillRemarkSubmitDTO submitDTO = new TShipBillRemarkSubmitDTO();
        submitDTO.setRemark(dto.getShipBillRemark());
        submitDTO.setTShipBillId(oneStockTally.getTShipBillId());
        submitDTO.setTransportBillId(oneStockTally.getTransportBillId());
        submitDTO.setTransportShipId(oneStockTally.getTransportShipId());
        return submitDTO;
    }

    private TShipBillRemarkSubmitDTO setTshipBillRemarkSubmitDTO(OneStockTally oneStockTally, OneStockTallyAppSubmitBatchDTO dto) {
        TShipBillRemarkSubmitDTO submitDTO = new TShipBillRemarkSubmitDTO();
        submitDTO.setRemark(dto.getShipBillRemark());
        submitDTO.setTShipBillId(oneStockTally.getTShipBillId());
        submitDTO.setTransportBillId(oneStockTally.getTransportBillId());
        submitDTO.setTransportShipId(oneStockTally.getTransportShipId());
        return submitDTO;
    }

    private OneStockTally detail(Long oneStockTallyId) {
        QueryWrapper<OneStockTally> oldTallyQuery = new QueryWrapper<>();
        oldTallyQuery.lambda()
                .eq(OneStockTally::getId, oneStockTallyId)
                .eq(OneStockTally::getIsDeleted, 0)
                .last("limit 1");
        OneStockTally oneStockTally = baseMapper.selectOne(oldTallyQuery);
        if (Func.isNull(oneStockTally)) {
            throw new RuntimeException(ExceptionDescription.DB_ERROR + "单垛理货记录不存在");
        }
        return oneStockTally;
    }

}
